//
//  MeatCopyViewController.m
//  PigMarket
//
//  Created by 张敬 on 2019/8/15.
//  Copyright © 2019年 pigmarket.oc. All rights reserved.
//

#import "MeatSaveViewController.h"
#import "SeparateDetailsViewController.h"
#import "MeatSendViewController.h"


#import "SeparatePlanCell.h"
#import "MeatPayView.h"

@interface MeatSaveViewController ()

@property (nonatomic, strong) MeatPayView *footer;
@property (nonatomic, strong) ShopSeparateModel *model;


@end

@implementation MeatSaveViewController


- (void)setupUI{
    
    {
        self.baseNavView.navTitleLabel.text = @"预留";
        self.baseNavView.navTitleLabel.textColor = KWhiteColor;
        
        [self.baseNavView.leftNavBtn setImage:[UIImage imageNamed:@"nav_white"] forState:UIControlStateNormal];
        
        UIImage *image = [UIImage imageNamed:@"meat_question_nav"];
        self.baseNavView.layer.contents =  (__bridge id)image.CGImage;
    }
    
    {
        [self.baseNavView.rightNavBtn setTitle:@"赠送列表" forState:UIControlStateNormal];
        self.baseNavView.rightNavBtn.width = 120;
        [self.baseNavView.rightNavBtn sizeToFit];
        self.baseNavView.rightNavBtn.mj_y = TJ_NavBarHeight - 42;
        self.baseNavView.rightNavBtn.mj_x = (KScreenWidth - self.baseNavView.rightNavBtn.width)-15;
        [self.baseNavView.rightNavBtn setTitleColor:KWhiteColor forState:UIControlStateNormal];
        self.baseNavView.rightNavBtn.hidden = NO;
        self.baseNavView.rightNavBtn.isNeedAnimation = YES;
        
        __block BOOL selected = NO;
        @weakify(self);
        self.baseNavView.rightNavBtn.tapBlock = ^{
            @strongify(self);
            if (selected) {
                selected = NO;
                [self.baseNavView.rightNavBtn setTitle:@"编辑" forState:UIControlStateNormal];
                self.footer.alpha = 0;
            }else{
                selected = YES;
                [self.baseNavView.rightNavBtn setTitle:@"完成" forState:UIControlStateNormal];
                self.footer.alpha = 1;
            }
        };
    }
    
    {
        [self.view addSubview:self.footer];
    }
    
}


- (void)setupLayout{
    
    {
        self.reloadBtn.mj_y += 30;
        self.reloadLab.mj_y += 30;
    }
    
    {
        self.footer.mj_y = self.view.bottom - _footer.height;
    }

}

#pragma mark - networking methods

- (void)initData{
    
    NSNumber *page = [NSNumber numberWithInteger:self.page];
    ShopMineSaleParam *param = [ShopMineSaleParam new];
    param.shopId = @0;
    param.memberId = kMemberId;
    param.pigId = @1;
    param.page = page;
    param.limit = @10;
    [self requestMyShopSaveByParam:param];
}


//我得店铺预留 Yes
- (void)requestMyShopSaveByParam:(ShopMineSaleParam *)param{
    
    @weakify(self);
    [[[MeatShopApi sharedInstance] getMyShopSaveByParams:param] subscribeNext:^(ShopSeparateModel * _Nullable model) {
        @strongify(self);
        
        self.model = model;
        
        if (self.isRefresh) {
            [self endRefresh];
        }
        
        if ([self.model.page.totalCount intValue] < [self.model.page.pageSize intValue] ) {
            [self endLoadMoreMoData];
        }else if ([self.model.page.totalCount intValue] == [self.model.page.pageSize intValue]){
            
            if ([self.model.page.currPage intValue] >= [self.model.page.totalCount intValue]) {
                [self endLoadMoreMoData];
            }else{
                [self endLoadMore];
            }
        }
        
        if (self.model.page.list.count >0) {
            self.tableView.hidden = NO;
            [self.tableView reloadData];
        }else{
            [self loadNoData];
        }
        
    }error:^(NSError * _Nullable error) {
        @strongify(self);
        [self loadErrorData];
    }];
}


//下拉刷新
- (void)refresh
{
    [super refresh];
    [self resetData];
    [self initData];
}

//上拉加载更多
- (void)loadMore
{
    [super loadMore];
    
    NSNumber *page = [NSNumber numberWithInteger:self.page];
    ShopMineSaleParam *param = [ShopMineSaleParam new];
    param.shopId = @0;
    param.memberId = kMemberId;
    param.pigId = @1;
    param.page = page;
    param.limit = @10;
    [self requestMyShopSaveByParam:param];
}



#pragma mark - tableview.delegate methods

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
}


#pragma mark - tableview.datascource methods

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.model.page.list.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    SeparatePlanCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SeparatePigCell"];
    if (self.model.page.list.count >0) {
        ShopSeparateList *item = self.model.page.list[indexPath.row];
        cell.item = item;
    }

    return cell;
}

#pragma mark - setter methods

#pragma mark - getter methods

- (void)registerTableViewCell
{
    self.tableView.rowHeight = 118;
    self.tableView.height = KScreenHeight - TJ_NavBarHeight -TJ_TabBarHeight;
    self.tableView.mj_y = TJ_NavBarHeight;
    self.tableView.separatorColor = mIconLineColor;
    [self.tableView registerClass:[SeparatePlanCell class] forCellReuseIdentifier:@"SeparatePigCell"];
}


- (MeatPayView *)footer{
    
    if (!_footer) {
        _footer = [[MeatPayView alloc] init];
        _footer.type = MeatPayType_SeparateCopy;
        
        //全选Block
        @weakify(self);
        _footer.chooseCartBlock = ^(UIButton *btn) {
            
        };
        
        //预留
        _footer.clearCartBlock = ^{
            @strongify(self);
            [[AlertAction sharedInstance] showAlertWithTitle:nil message:@"您确定要全部预留吗？，一经预留不可更改" confirmBtnTitle:@"确定" confirmBlock:^{
                // [self clearLoginAccount];
            } cancelBlock:nil];
        };
        
        //销售
        _footer.deleteCartBlock = ^{
            @strongify(self);
            MeatSendViewController *vc = [[MeatSendViewController alloc] init];
            [self.navigationController pushViewController:vc animated:YES];
        };
    }
    return _footer;
}


- (ShopSeparateModel *)model{
    
    if (!_model) {
        _model = [[ShopSeparateModel alloc] init];
    }
    return _model;
}

#pragma mark override methods

- (void)loadErrorData
{
    [super loadErrorData];
    self.footer.hidden = YES;
}

- (void)loadNoData
{
    [super loadNoData];
    self.footer.hidden = YES;
}


- (void)endLoadMore{
    [super endLoadMore];
    self.footer.hidden = NO;
}

- (void)endRefresh{
    [super endRefresh];
    self.footer.hidden = NO;
}

- (void)endLoadMoreMoData
{
    [super endLoadMoreMoData];
    self.footer.hidden = NO;
}


@end

